package t20220307;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author: DengJia
 * @Date: 2022/3/7 20:18
 * @Description:
 */

public class Regex2 {
    private static final Pattern PA = Pattern.compile("\\w+");
    private static final Pattern REMOVE_EXTRA_COMMA = Pattern.compile("^,*|,*$");

    /**
     * 大小写不敏感匹配
     */
    private static final Pattern PCI = Pattern.compile("\\w", Pattern.CASE_INSENSITIVE);

    /**
     * 多行模式下，^$分别匹配一行的开始和结束，^$还匹配字符串的开始和和结尾
     */
    private static final Pattern PM = Pattern.compile("\\w", Pattern.MULTILINE);

    /**
     * 空格以及以#开头直至行末的注释会被忽略（对于声明和文档有用）
     */
    private static final Pattern PC = Pattern.compile("\\w", Pattern.COMMENTS);

    /**
     * 使用|组合多个标记的功能
     */
    private static final Pattern PCI_M_C =
            Pattern.compile("^java", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.COMMENTS);

    private static final String S1 = "Java now has regular expressions";

    public static void main(String[] args) {
        // pa();
        pcimc();
    }

    public static void pcimc() {
        String input = "java has regex\nJava has regex\n" +
                "JAVA has pretty good regular expressions\n" +
                "Regular expressions are in Java\n" +
                "# Java comments: PCI_M_C模式匹配以java开头的行，并且是在设置了多行标记的状态下，对每一个行都进行匹配\n" +
                "# JAVA COMMENTS: 注意 group()方法只返回以匹配的部分";
        Matcher m = PCI_M_C.matcher(input);
        while (m.find()) {
            System.out.println(m.group());
        }

        // reset方法可以将模式应用于一个新的字符序列，
        // 使用不带参数的reset()方法，将Matcher对象重新设置到当前字符序列的起始位置。

    }

    public static void pa() {
        Matcher m = PA.matcher(S1);
        while (m.find()) {
            System.out.println(m.group());
        }

        int i = 0;
        while (m.find(i)) {
            System.out.println(m.group());
            i++;
        }
    }
}
